package org.argeproje.resim.proc.view;


import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.image.BufferedImage;
import java.awt.image.renderable.ParameterBlock;
import java.util.ArrayList;
import java.util.Iterator;

import javax.media.jai.JAI;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import org.argeproje.resim.ResimProjectEditor;
import org.argeproje.resim.ViewerPage;
import org.argeproje.resim.proc.ConnectionRule;
import org.argeproje.resim.proc.data.Data;
import org.argeproje.resim.proc.data.HistogramDA;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.DefaultXYDataset;
import org.jfree.data.xy.XYBarDataset;

public class HistogramViewerPR extends ViewerPR {
	protected void setConnectionRules(ConnectionRule connRule){
		connRule.addInType(HistogramDA.class, 0);
	}
	@Override
	public Data process() {
		HistogramDA data = getInputAsHistogramDA();
        int nBins = data.getNBins();
        int nBands = data.getNBands();

		for (int i = 0; i < nBands; i++) {
			DefaultXYDataset xySet = new DefaultXYDataset();
			double[][] points = new double[2][nBins];
			int[] histogram = data.getBins(i);
			for(int y = 0; y < nBins; y++){
					points[0][y] = y;
					points[1][y] = (double)histogram[y];
			}
			xySet.addSeries("HI", points);
			XYBarDataset series = new XYBarDataset(xySet, 2);
			JFreeChart chart = ChartFactory.createXYBarChart(
					"Histogram Chart", (i+1) + ". Band Bins", false, "Total Numbers", series,
					PlotOrientation.VERTICAL, true, false, false);
			XYPlot plot = (XYPlot) chart.getPlot();
			// set the range axis to display integers only...
			final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
			rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
			ChartPanel panel = new ChartPanel(chart);
			setViewPanel(panel,i,panel.toString(),"Histogram Viewer");
		}
		return null;
	}	
	public void zoomOut(){
		((ChartPanel)getShowingPanel()).zoomOutBoth(1.0, 1.0);
	}
	public void  zoomIn(){
		((ChartPanel)getShowingPanel()).zoomInBoth(1.0, 1.0);
	}
	public void save(String fileName){
		ChartPanel panel = (ChartPanel)getShowingPanel();
		ParameterBlock pb = new ParameterBlock();
		BufferedImage im = panel.getChart().createBufferedImage(panel.getWidth(), panel.getHeight(), BufferedImage.TYPE_INT_RGB, null);
		pb.addSource(im);
		pb.add(fileName);
		pb.add("JPEG");
		JAI.create("filestore", pb, null);		
	}
}
